// ignore_for_file: avoid_print
import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter_base_library/index.dart';
import 'package:safe_manage/models/index.dart';
import 'package:tpns_flutter_plugin/tpns_flutter_plugin.dart';

class MyAppData {
  static final MyAppData instance = MyAppData();
  // String? baseUrl;
  // String? token;
  String? xgToken;
  // UserInfo? currentUserInfo;
  User? _currentUser;
  final XgFlutterPlugin tpush = XgFlutterPlugin();
  set currentUser(User? user) {
    _currentUser = user;
    initPlatformState();
  }

  User? get currentUser {
    return _currentUser;
  }

  void initXGToken() async {
    var userid = _currentUser?.id;
    if (userid == null || xgToken == null) {
      return;
    }
    DeviceInfoPlugin infoPlugin = DeviceInfoPlugin();
    AndroidDeviceInfo? deviceInfo;
    if (Platform.isAndroid) {
      deviceInfo = await infoPlugin.androidInfo;
    }
    KVDataUtils kvDataUtils = KVDataUtils.getInstance();
    String? androidId = kvDataUtils.getString("androidId");
    if (androidId == null) {
      androidId = "${_currentUser!.id}_";
      androidId += DateTime.now().millisecondsSinceEpoch.toString();
      kvDataUtils.setString("androidId", androidId);
    }
    var p = {
      "current": 0,
      "size": 0,
      "parameter": {
        "gsid": _currentUser!.gsid,
        "token": xgToken,
        "userid": userid,
        "phoneMeid": androidId,
        "phoneOs": "Android",
        "phoneFactory": deviceInfo?.manufacturer,
      }
    };
    HttpUtils.post(
      '/safety-web/appUser/saveOrUpdate',
      body: p,
      success: (result) {
        // print(result);
      },
      fail: (errorMsg) {
        // print(errorMsg);
      },
    );
  }

  Future<void> initPlatformState() async {
    /// 开启DEBUG
    tpush.setEnableDebug(true);

    /// 添加回调事件
    tpush.addEventHandler(
      onRegisteredDeviceToken: (String msg) async {
        print("flutter onRegisteredDeviceToken: $msg");
      },
      onRegisteredDone: (String msg) async {
        print(
          "----------------flutter onRegisteredDone: $msg ------------------",
        );
        xgToken = msg;
        initXGToken();
      },
      unRegistered: (String msg) async {
        print("flutter unRegistered: $msg");
      },
      onReceiveNotificationResponse: (Map<String, dynamic> msg) async {
        print("flutter onReceiveNotificationResponse $msg");
      },
      onReceiveMessage: (Map<String, dynamic> msg) async {
        print("flutter onReceiveMessage $msg");
      },
      xgPushDidSetBadge: (String msg) async {
        print("flutter xgPushDidSetBadge: $msg");

        /// 在此可设置应用角标
        /// XgFlutterPlugin().setAppBadge(0);
      },
      xgPushDidBindWithIdentifier: (String msg) async {
        print("flutter xgPushDidBindWithIdentifier: $msg");
      },
      xgPushDidUnbindWithIdentifier: (String msg) async {
        print("flutter xgPushDidUnbindWithIdentifier: $msg");
      },
      xgPushDidUpdatedBindedIdentifier: (String msg) async {
        print("flutter xgPushDidUpdatedBindedIdentifier: $msg");
      },
      xgPushDidClearAllIdentifiers: (String msg) async {
        print("flutter xgPushDidClearAllIdentifiers: $msg");
      },
      xgPushClickAction: (Map<String, dynamic> msg) async {
        print("flutter xgPushClickAction $msg");
      },
    );

    /// 如果您的应用非广州集群则需要在startXG之前调用此函数
    /// 香港：tpns.hk.tencent.com
    /// 新加坡：tpns.sgp.tencent.com
    /// 上海：tpns.sh.tencent.com
    // XgFlutterPlugin().configureClusterDomainName("tpns.hk.tencent.com");
    /// 启动TPNS服务
    // XgFlutterPlugin().startXg("1500035917", "AO0DRJLT69SF"); // 我的测试通道
    tpush.startXg("1500035851", "AO346FOXG39Q"); // 线上配置
    XgFlutterPlugin.xgApi.enableOtherPush();
    XgFlutterPlugin.xgApi.regPush();
  }
}
